package com.ljb.commlibrary.http;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.ljb.commlibrary.utils.L;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 图片请求工具类
 * Created by ljb on 2018/5/4.
 */

public class ImageUtils {

    public static void load(FragmentActivity activity, String url, int placeholder, ImageView iv) {
        Glide.with(activity)
                .load(urlEncode(url))
                .apply(initOptions(placeholder, placeholder))
                .into(iv);
    }

    public static void load(Fragment fragment, String url, int placeholder, ImageView iv) {
        Glide.with(fragment)
                .load(urlEncode(url))
                .apply(initOptions(placeholder, placeholder))
                .into(iv);
    }

    public static void load(Context context, String url, int placeholder, ImageView iv) {
        Glide.with(context)
                .load(urlEncode(url))
                .apply(initOptions(placeholder, placeholder))
                .into(iv);
    }

    public static void load(View view, String url, int placeholder, ImageView iv) {
        Glide.with(view)
                .load(urlEncode(url))
                .apply(initOptions(placeholder, placeholder))
                .into(iv);
    }

    private static RequestOptions initOptions(int placeholder, int error) {
        return new RequestOptions()
                .placeholder(placeholder)
                .error(error)
                .diskCacheStrategy(DiskCacheStrategy.NONE);
    }

    /**
     * 中文转换utf-8,解决中文路径问题
     *
     * @param url
     * @return
     */
    public static String urlEncode(String url) {
        StringBuilder encodeUrl = new StringBuilder();
        try {
            char[] ch = url.toCharArray();
            for (char c : ch) {
                Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
                if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                        || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                        || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                        || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                        || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                        || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                        || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
                    encodeUrl.append(URLEncoder.encode(String.valueOf(c), "UTF-8"));
                } else {
                    encodeUrl.append(c);
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        L.i("encodeUrl="+encodeUrl.toString());
        return encodeUrl.toString();
    }
}


































